import numpy as np
import torch

class AdaptiveNoiseInjector:
    def __init__(self, base_noise_scale=1.0, min_lr=0.001, max_lr=0.1):
        self.base_noise_scale = base_noise_scale
        self.min_lr = min_lr
        self.max_lr = max_lr
        self.noise_history = []
        
    def inject_noise(self, predicted_norm, clip_threshold, clipped_grad, remaining_budget):
        """自适应噪声注入算法"""
        # Step 1: 噪声缩放因子计算
        if len(self.noise_history) > 0:
            base_norm = np.mean(self.noise_history)
        else:
            base_norm = 1.0
        scale_factor = base_norm / (predicted_norm + 1e-8)
        scale_factor = np.tanh(scale_factor)  # 双曲正切平滑
        
        # Step 2: 噪声注入执行
        noise_scale = self.base_noise_scale * scale_factor
        noise = torch.rand
